package ynu.beggar.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import ynu.beggar.server.entities.DO.ForumPostCreateDO;
import ynu.beggar.server.entities.DO.ForumPostDelDO;
import ynu.beggar.server.entities.DO.UserBasicInfoDO;
import ynu.beggar.server.service.ForumService;
import ynu.beggar.server.utils.JsonData;

@RestController
@RequestMapping(path = "/api/v1/forum", produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*")
public class ForumController {

    private ForumService forumService;

    @Autowired
    private void setForumService(ForumService forumService) {
        this.forumService = forumService;
    }

    @GetMapping(path = "/recentPosts")
    public JsonData getPostList(@PageableDefault(page = 0, value = 12, sort = {"createTime"}, direction = Sort.Direction.DESC) Pageable pageable, @RequestParam(name = "type", defaultValue = "h") String type) {
        return forumService.queryPostPage(pageable, type);
    }

    @GetMapping(path = "/recentBulletins")
    public JsonData getBulletins() {
        return forumService.queryBulletins();
    }


    //#region 登录后可操作
    @PostMapping(path = "/user/newPost", consumes = {"application/json"})
    public JsonData newPost(@RequestBody ForumPostCreateDO forumPostCreateDO, @RequestAttribute UserBasicInfoDO userBasicInfoDO) {
        return forumService.createPost(forumPostCreateDO, userBasicInfoDO);
    }

    @PostMapping(path = "/user/delPost", consumes = {"application/json"})
    public JsonData delPost(@RequestBody ForumPostDelDO forumPostDelDO, @RequestAttribute UserBasicInfoDO userBasicInfoDO) {
        return forumService.deletePost(forumPostDelDO, userBasicInfoDO);
    }

    @GetMapping(path = "/user/myPost")
    public JsonData getMyPost(@PageableDefault(page = 0, value = 12, sort = {"createTime"}, direction = Sort.Direction.DESC) Pageable pageable, @RequestAttribute UserBasicInfoDO userBasicInfoDO) {
        return forumService.queryMyPostPage(pageable, userBasicInfoDO);
    }

    @GetMapping(path = "/user/postDetail")
    public JsonData getPostDetail(@RequestParam(name = "postId") int postId) {
        return forumService.queryPostDetail(postId);
    }
    //#endregion
}
